package com.cloud.controller;


import com.cloud.pojo.File;
import com.cloud.pojo.User;
import com.cloud.service.FileService;
import com.cloud.vo.CloudResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 上传文件表 前端控制器
 * </p>
 *
 * @author haoyeee123
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    /*
    * 文件上传
    * */
    @PostMapping("/add")
    public CloudResult addFile(@RequestParam("uid") Integer uid,@RequestParam("file") MultipartFile file){
        fileService.addFile(uid,file);
        return CloudResult.success();
    }
    /*
    * 文件加载
    * */
    @PostMapping("/list")
    public CloudResult getList(@RequestBody File file){
        List<File> files = fileService.getList(file);
        return CloudResult.success(files);
    }
    /*
    * 文件下载
    * */
    @GetMapping("/download")
    public CloudResult getFile(HttpServletRequest request,HttpServletResponse response, File file ){
        fileService.getFile(request,response,file);
        return CloudResult.success();
    }
    /*
    * 文件删除
    * */
    @PostMapping("/delete")
    public CloudResult deleteFile(@RequestBody File file){
        fileService.deleteFile(file);
        return  CloudResult.success();
    }

}

